**************************************************************************************************************************
*****************************************Programs for the Study*********************************************************

*******************Part I Simulation Setting I to Setting V*************************
1. Generate Datasets for setting I to V. R
    - Used to generate 200 datasets for each setting

2. Macro program setting I to IV. sas (Need quant_r.txt and quant_d.txt to run)
   - Estimate the model parameters under settings I to IV

3. Macro program gap time and total time for setting V. sas (Need quant_r V.txt and quant_d.txt to run)
   - Estimate the model parameters using gap time model and total time model for settings V
 
4. Summary the fitting results from setting I to IV.R
   -Summary the estimation of model parameters under settings I to IV
   - Results shown in Table S1-S4



*******************Part II Self Defined Function*************************
1. Lambdainv.R
    -Function used to find vector T such that Lambda(T)=s for a vector input s

2. myprod.R
    -Function used to product of two step function

3. mysimRec
   -Function using Cinlar's inversion Method to generate Non-homogeneous Poisson process

4. mysimRec_gap
   -Function using Cinlar's inversion Method to generate Non-homogeneous Poisson process for gap time and then convert to study time

5. datagen_M_gap.R
   -Function used to generate recurrent events M0 and M1

6. datagen_X_gap.R
   -Function used to generate datasets with simulated X and Z

7. datagenQ13_M_gap.R
   -Function used to generate recurrent events M_Q1 and M_Q3
   -Q1 and Q3 are two comparing categories for NDE/NIE

8. datagen_M.R
   -Function used to generate recurrent events M0 and M1 under total time model

9. mymed.R
   -Function used to compute the NDE/NIE for simulation settings I-IV

10. mymed_V.R
   -Function used to compute the NDE/NIE for simulation settings V

11. mymed_Q13.R
   -Function used to compute the NDE/NIE with Z containing two categories Q1 and Q3

12. mymed_newlog.R
   -Function used to compute the NDE/NIE for total time model

13. S.R
    - Used to compute the survival functions

14. S00.R
     - Compute the survival functions S00

15. S01.R
     - Compute the survival functions S01

16. S10.R
     - Compute the survival functions S10
 
17. S11.R
     - Compute the survival functions S11
 
   

*******************Part III Estimate NDE/NIE Under Simulation Setting I to IV*************************

1. Folder "NDE NIE from Simulation I"
    *3-1. SimulationI_parallel_gap.R
              - Function used to estimate NDE/NIE under simulation setting I
    *3-2. SimulationI_parallel_boot_gap.R
             -Function of the bootstrap for NDE/NIE under simulation setting I
     *3-3. SimulationI_summary_gap.R
             -Function used to summary and estimate bias, SD, Mese and CR for NDE/NIE under simulation setting I


2. Folder "NDE NIE from Simulation II"
    *4-1. SimulationII_parallel_gap.R
              - Function used to estimate NDE/NIE under simulation setting II
    *4-2. SimulationII_parallel_boot_gap.R
             -Function of the bootstrap for NDE/NIE under simulation setting II
    *4-3. SimulationII_summary_gap.R
             -Function used to summary and estimate bias, SD, Mese and CR for NDE/NIE under simulation setting II


3. Folder "NDE NIE from Simulation III"
    *5-1. SimulationIII_parallel_gap.R
              - Function used to estimate NDE/NIE under simulation setting III
    *5-2. SimulationIII_parallel_boot_gap.R
             -Function of the bootstrap for NDE/NIE under simulation setting III
    *5-3. SimulationIII_summary_gap.R
             -Function used to summary and estimate bias, SD, Mese and CR for NDE/NIE under simulation setting III


4. Folder "NDE NIE from Simulation IV"
    *6-1. SimulationIV_parallel_gap.R
              - Function used to estimate NDE/NIE under simulation setting IV
    *6-2. SimulationIV_parallel_boot_gap.R
             -Function of the bootstrap for NDE/NIE under simulation setting IV
    *6-3. SimulationIV_summary_gap.R
             -Function used to summary and estimate bias, SD, Mese and CR for NDE/NIE under simulation setting IV

*******************Part IV Compare the Estimatation of NDE/NIE using Gap Time or Total Time Model Under Simulation Setting V*************************

1. Folder "NDE NIE from Simulation V"
    *7-1. SimulationV_parallel_gap.R
              - Function used to estimate NDE/NIE under simulation setting V
    *7-2. SimulationV_parallel_boot_gap.R
             -Function of the bootstrap for NDE/NIE under simulation setting V
    *7-3. SimulationV_summary_gap.R
             -Function used to summary and estimate bias, SD, Mese and CR for NDE/NIE under simulation setting V

  *8-1. SimulationV_parallel.R
              - Function used to estimate NDE/NIE using Total Time model for simulation setting V
    *8-2. SimulationV_parallel_boot.R
             -Function of the bootstrap for NDE/NIE  using Total Time model for simulation setting V
    *8-3. SimulationV_summary.R
             -Function used to summary and estimate bias, SD, Mese and CR for NDE/NIE using Total Time model for simulation setting V

*******************Part V Real Data Analysis*************************

1. CPCRA study analysis.sas
    -Estimates the parameters under simulation setting I and II for CPCRA study

2. Folder "CPCRA NDE NIE Plot"

   * NDE_NIE_Gap_Trt.R 
      -Estimate NDE/NIE for treatment

   * NDE_NIE_Gap_CI_Trt.R
     -Boostrap of NDE/NIE for treatment

   * Summary_Gap_Trt.R
    -Summary the results and plot the estimates of NDE/NIE/TE with bootstrapped 95% CI for treatment

  * NDE_NIE_Gap_CD4.R 
      -Estimate NDE/NIE for CD4

   * NDE_NIE_Gap_CI_CD4.R
     -Bootstrap of NDE/NIE for CD4

   * Summary_Gap_CD4.R
    -Summary the results and plot the estimates of NDE/NIE/TE with bootstrapped 95% CI for CD4



**************************************************************************************************************************
******************Instructions for using the program to generate manuscript tables and figures**************************

*******************Part I Simulation Flowchart*************************

First, run the file "Generate Datasets for setting I to V. R" to obtain simulated data;

Second, run the file "Macro program setting I to IV. sas" and "Macro program gap time and total time for setting V. sas" to fit parameters using the simulated data;

Third, run the files in folder "Folder "NDE NIE from Simulation I", "Folder "NDE NIE from Simulation II", "Folder "NDE NIE from Simulation III","Folder "NDE NIE from Simulation IV", "Folder "NDE NIE from Simulation V" to obtain NDE, NIE using the fitted parameters and summarize the results and obtain Table 1 and 2.


Comment: The computing time is long and need approxiamte 150 days for the full simulation using 1000 numbers of node/CPUs. 

Real data analysis flowchart:
First, run the file "CPCRA study analysis.sas" to preprocess data and fit parameters using the data for Table 3;

Second, run the files in Folder "CPCRA NDE NIE Plot" to obtain NDE, NIE using the fitted parameters, and summarize the results and obtain Figure 1 and 2.

Comment: The data is not publicly available due to license restriction and cannot be shared.


 